/*
 * Created on 12/Abr/2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package org.cyborgas.xmltool.actions;

import java.io.File;
import java.net.URI;

import org.cyborgas.xmltool.MainWindow;
import org.cyborgas.xmltool.Util;
import org.cyborgas.xmltool.tabsheets.XslTransformTab;
import org.eclipse.jface.action.Action;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;


/**
 * @author rino
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class XslSaveAction extends Action {
	private MainWindow window;
	private XslTransformTab tab;

	public XslSaveAction(MainWindow window, XslTransformTab tab) {
		this.window = window;
		this.tab = tab;
		setText("&Save stylesheet");
		setToolTipText("Save XSL file");
		setImageDescriptor(Util.getImageDescriptor("filesave"));
	}

	public void run() {
		URI url = null;
		if (this.tab.xslFile == null) { 
			FileDialog dialog =
				new FileDialog(window.getShell(), SWT.SAVE);
			dialog.setFilterExtensions(new String[] {"*.xsl", "*.*"});
			dialog.open();
		
			String fileName = dialog.getFileName();
			if (fileName != null && !fileName.equals("")) {
				url = new File(dialog.getFilterPath(), fileName).toURI();
			}
		} else {
			url = this.tab.xslFile;
		}
		if (url != null) {
			tab.save(url);
		}
	}	

}
